/**
 *====================================================
 * 文件名称: BulkstockCategoryServiceImpl.java
 * 修订记录：
 * No    日期				作者(操作:具体内容)
 * 1.    2015年09月23日	chenxy(创建:创建文件)
 *====================================================
 * 类描述：(说明未实现或其它不应生成javadoc的内容)
 */
package com.yuexunit.finance.bulkstock.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;

import com.yuexunit.core.BusinessException;
import com.yuexunit.finance.bulkstock.entity.BulkstockCategoryEntity;
import com.yuexunit.finance.bulkstock.entity.BulkstockCategoryFinance;
import com.yuexunit.finance.common.util.DateUtils;
import com.yuexunit.finance.project.entity.FinanceApplicationEntity;
import com.yuexunit.finance.project.entity.FinanceBulkstockEntity;
import com.yuexunit.inject.annotation.Implements;
import com.yuexunit.page.PageCriteria;
import com.yuexunit.page.PageList;
import com.yuexunit.persist.PersistUtil;
import com.yuexunit.persist.annotation.Transactional;
import com.yuexunit.util.DateUtil;
import com.yuexunit.util.StringUtil;

/**
 * @ClassName: BulkstockCategoryServiceImpl
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author chenxy
 * @date 2015年09月23日 18:53:27
 */
@Implements(BulkstockCategoryService.class)
public class BulkstockCategoryServiceImpl implements BulkstockCategoryService {

	/** 重复验证 */
	private void validate(BulkstockCategoryEntity entity) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockCategoryEntity.class);
		dc.add(Restrictions.eq("categoryName", entity.getCategoryName()));
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		List<BulkstockCategoryEntity> list = PersistUtil.get(dc);
		if (!list.isEmpty()) {
			throw new BusinessException("大宗商品类别已存在");
		}
	}

	@Override
	@Transactional
	public Long createBulkstockCategory(BulkstockCategoryEntity entity) {
		validate(entity);
		entity.setEnabled(Boolean.TRUE);
		entity.setCreateDateTime(DateUtil.now());
		entity.setUpdateDateTime(DateUtil.now());
		entity.setActiveState(Boolean.FALSE);
		PersistUtil.insert(entity);
		return entity.getBulkstockCategoryId();
	}

	@Override
	@Transactional
	public void updateBulkstockCategory(BulkstockCategoryEntity entity) {
		BulkstockCategoryEntity be = PersistUtil.get(BulkstockCategoryEntity.class, entity.getBulkstockCategoryId());
		be.setCategoryName(entity.getCategoryName());
		be.setUpdateDateTime(DateUtils.now());
		PersistUtil.update(be);
	}
	
	@Override
	@Transactional
	public void activeBulkstockCategory(Long bulkstockCategoryId) {
		BulkstockCategoryEntity entity = PersistUtil.get(BulkstockCategoryEntity.class, bulkstockCategoryId);
		entity.setActiveState(Boolean.TRUE);
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void deprecatedBulkstockCategory(Long bulkstockCategoryId) {
		BulkstockCategoryEntity entity = PersistUtil.get(BulkstockCategoryEntity.class, bulkstockCategoryId);
		entity.setActiveState(Boolean.FALSE);
		PersistUtil.update(entity);
	}

	@Override
	@Transactional
	public void disableBulkstockCategory(Long bulkstockCategoryId) {
		BulkstockCategoryEntity entity = PersistUtil.get(BulkstockCategoryEntity.class, bulkstockCategoryId);
		entity.setEnabled(Boolean.FALSE);
		entity.setUpdateDateTime(DateUtil.now());
		PersistUtil.update(entity);
	}

	@Override
	public BulkstockCategoryEntity inquireBulkstockCategoryDetail(Long bulkstockCategoryId) {
		BulkstockCategoryEntity e = PersistUtil.get(BulkstockCategoryEntity.class, bulkstockCategoryId);
		if (e == null || !e.getEnabled()) {
			throw new BusinessException("大宗商品类别不存在");
		}
		return e;
	}

	@Override
	public BulkstockCategoryEntity inquireBulkstockCategory(Long bulkstockCategoryId) {
//		Session session = SessionRepository.getCurrentSession();
//		Transaction tx = session.beginTransaction();
//		String sql = "select * from FIN_ACCOUNT_TERMINAL";
//		List<Object> list = session.createSQLQuery(sql).addScalar("ACCOUNT_ID").addScalar("TERMINAL_SERIAL").list();
//		List<Object> list = session.createSQLQuery(sql)..list();
//		for (Object l : list) {
//			AccountTerminalEntity ate = (AccountTerminalEntity)l;
//			System.out.println(ate);
//			Object[] os = (Object[])l;
//			System.out.println(l);
//			System.out.println(os[0]);
//			System.out.println(os[1]);
//			A ate = (A)l;
//			System.out.println(ate);
//		}
		return PersistUtil.get(BulkstockCategoryEntity.class, bulkstockCategoryId);
	}

	@Override
	public PageList<BulkstockCategoryEntity> inquireBulkstockCategoryList(String categoryName, PageCriteria page) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockCategoryEntity.class);
		if (StringUtils.isNotBlank(categoryName)) {
			dc.add(Restrictions.like("categoryName", "%" + categoryName + "%"));
		}
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		dc.add(Restrictions.eq("activeState", Boolean.TRUE));
		return PersistUtil.get(dc, page);
	}

	@Override
	public BulkstockCategoryEntity inquireBulkstockCategory(String categoryName) {
		DetachedCriteria dc = DetachedCriteria.forClass(BulkstockCategoryEntity.class);
		dc.add(Restrictions.eq("categoryName", categoryName));
		dc.add(Restrictions.eq("enabled", Boolean.TRUE));
		List<BulkstockCategoryEntity> list = PersistUtil.get(dc);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public PageList<BulkstockCategoryEntity> inquireBulkstockCategoryList(Long bulkstockCategoryId, Date beginUpdateDate, Date endUpdateDate, Boolean activeState, String categoryName, Boolean isFactoring, PageCriteria page) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockCategoryEntity.class);
		criteria.add(Restrictions.eq("enabled", Boolean.TRUE));
		if (bulkstockCategoryId != null) {
			criteria.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		}
		if (activeState != null) {
			criteria.add(Restrictions.eq("activeState", activeState));
		}
		if (beginUpdateDate != null) {
			criteria.add(Restrictions.ge("updateDateTime", beginUpdateDate));
		}
		if (endUpdateDate != null) {
			criteria.add(Restrictions.le("updateDateTime", endUpdateDate));
		}
		if (StringUtil.isNotBlank(categoryName)) {
			criteria.add(Restrictions.like("categoryName", "%" + categoryName + "%"));
		}
		if (isFactoring != null) {
			criteria.add(Restrictions.eqOrIsNull("isFactoring", isFactoring));
		}
		return PersistUtil.get(criteria, page);

	}

	@Override
	public BulkstockCategoryEntity inquireFIDBulkstockCategory(String categoryFID, String categoryName) {
		DetachedCriteria criteria = DetachedCriteria.forClass(BulkstockCategoryEntity.class);
		criteria.add(Restrictions.or(Restrictions.eq("categoryFID", categoryFID), Restrictions.eq("categoryName", categoryName)));
		List<BulkstockCategoryEntity> list = PersistUtil.get(criteria);
		if (list == null || list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}

	@Override
	public List<BulkstockCategoryFinance> existUnfinishFinance(
			Long bulkstockCategoryId) {
		DetachedCriteria criteria = DetachedCriteria.forClass(FinanceBulkstockEntity.class);
		criteria.add(Restrictions.eq("bulkstockCategoryId", bulkstockCategoryId));
		List<FinanceBulkstockEntity> financeBulkstockEntities = PersistUtil.get(criteria);
		List<BulkstockCategoryFinance> list = new ArrayList<BulkstockCategoryFinance>();
		BulkstockCategoryFinance bulkstockCategoryFinance = null;
		for (FinanceBulkstockEntity financeBulkstockEntity : financeBulkstockEntities) {
			bulkstockCategoryFinance = new BulkstockCategoryFinance();
			FinanceApplicationEntity financeApplicationEntity =	PersistUtil.get(FinanceApplicationEntity.class, financeBulkstockEntity.getFinanceId());
			bulkstockCategoryFinance.setBulkstockInfoId(financeBulkstockEntity.getBulkstockId());
			bulkstockCategoryFinance.setBulkstockName(financeBulkstockEntity.getBulkstockName());
			bulkstockCategoryFinance.setFinanceId(financeApplicationEntity.getFinanceId());
			bulkstockCategoryFinance.setFinanceState(financeApplicationEntity.getFinanceState());
			list.add(bulkstockCategoryFinance);
		}
		return list;
	}
}
